<?php

namespace App\Http\Controllers\Counter;

use App\Enums\MemberBalanceType;
use App\Enums\OrderFrom;
use App\Enums\OrderType;
use App\Enums\PayChannel;
use App\Enums\TradeStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\Counter\MemberGeneralRechargeRequest;
use App\Http\Requests\Counter\MemberTableRechargeRequest;
use App\Http\Resources\Counter\OnlineTradeOrderResource;
use App\LeShua\LeShua;
use App\Models\Member;
use App\Models\OnlineTradeOrder;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class MemberRechargeController extends Controller
{
    public function general(Member $member, MemberGeneralRechargeRequest $request)
    {
        try {
            $payChannel = PayChannel::from($request->type);

            DB::transaction(function () use ($payChannel, $member, $request, &$tradeOrder) {

                //先创建充值订单
                /**
                 * @var Order $order
                 */
                $order = Order::create([
                    'order_no' => Order::generateOrderNo(),
                    'user_id' => Auth::id(),
                    'member_id' => $member->id,
                    'store_id' => Auth::user()->store_id,
                    'status' => TradeStatus::Created,
                    'money' => $request->balance,
                    'pay_channel' => $payChannel,
                    'comment' => '会员储值',
                    'type' => OrderType::MemberRecharge,
                    'from' => OrderFrom::Counter,
                    'give_money' => $request->balance_give,
                    'origin_money' => $request->balance,
                    'member_balance_type' => MemberBalanceType::General,
                    'give_table_money' => $request->table_balance_give,
                ]);

                if ($payChannel == PayChannel::LeShua) {

                    /**
                     * @var OnlineTradeOrder $tradeOrder
                     */
                    $tradeOrder = OnlineTradeOrder::create([
                        'order_no' => OnlineTradeOrder::genOrderNo(),
                        'order_ids' => [$order->id],
                        'total_money' => $order->money,
                        'status' => TradeStatus::Created,
                        'store_id' => Auth::user()->store_id,
                    ]);

                    $store = Auth::user()->store;
                    $payment = new LeShua($store->leshua_num);
                    $response = $payment->scanPay($request->bar_code, $tradeOrder->order_no, $order->money * 100);

                    if ($response->getStatus() == 2)
                        $tradeOrder->payed();
                    else
                        $tradeOrder->wait();
                } else {
                    $order->orderPayed($payChannel);
                }
            });

            if ($payChannel == PayChannel::LeShua)
                return new OnlineTradeOrderResource($tradeOrder);

            return ApiResponseHelper::json([]);

        } catch (\Exception $exception) {
            return ApiResponseHelper::json([], 400, $exception->getMessage());
        }
    }

    public function table(Member $member, MemberTableRechargeRequest $request)
    {
        try {
            $payChannel = PayChannel::from($request->type);

            DB::transaction(function () use ($member, $payChannel, $request, &$tradeOrder) {

                //先创建充值订单
                /**
                 * @var Order $order
                 */
                $order = Order::create([
                    'order_no' => Order::generateOrderNo(),
                    'user_id' => Auth::id(),
                    'member_id' => $member->id,
                    'store_id' => Auth::user()->store_id,
                    'status' => TradeStatus::Created,
                    'money' => $request->real_balance,
                    'pay_channel' => $payChannel,
                    'comment' => '会员储值',
                    'type' => OrderType::MemberRecharge,
                    'from' => OrderFrom::Counter,
                    'give_money' => $request->balance,
                    'origin_money' => $request->real_balance,
                    'member_balance_type' => MemberBalanceType::Table,
                    'give_table_money' => 0,
                ]);

                if ($payChannel == PayChannel::LeShua) {

                    /**
                     * @var OnlineTradeOrder $tradeOrder
                     */
                    $tradeOrder = OnlineTradeOrder::create([
                        'order_no' => OnlineTradeOrder::genOrderNo(),
                        'order_ids' => [$order->id],
                        'total_money' => $order->money,
                        'status' => TradeStatus::Created,
                        'store_id' => Auth::user()->store_id,
                    ]);

                    $store = Auth::user()->store;
                    $payment = new LeShua($store->leshua_num);
                    $response = $payment->scanPay($request->bar_code, $tradeOrder->order_no, $order->money * 100);

                    if ($response->getStatus() == 2)
                        $tradeOrder->payed();
                    else
                        $tradeOrder->wait();
                } else {
                    $order->orderPayed($payChannel);
                }
            });

            if ($payChannel == PayChannel::LeShua)
                return new OnlineTradeOrderResource($tradeOrder);

            return ApiResponseHelper::json([]);

        } catch (\Exception $exception) {
            return ApiResponseHelper::json([], 400, $exception->getMessage());
        }
    }
}
